package org.idea.qiyu.common;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 数据库id生成器组件
 *
 * @Author linhao
 * @Date created in 7:11 下午 2021/6/13
 */
public class DatabaseIdGenerateUtils {

    /**
     * 机器编号，每个不同节点的服务都会有一个id
     */
    private static final int machineIndex = 19;

    private static AtomicLong index = new AtomicLong(0);

    private static final int datacenterSize = 5;

    private static String randomNum = "";

    static {
        randomNum = String.valueOf(Math.random());
    }



    /**
     * id 生成组件
     *
     * @return
     */
    public synchronized String idGenerate(){
        long timestamp = System.currentTimeMillis();
        long currentIndex = index.getAndIncrement();
        String idPrefix =  machineIndex +""+currentIndex;
        char[] chars = idPrefix.toCharArray();
        String result = "";
        for(int i=0;i<chars.length;i++){
            result += Integer.toBinaryString(chars[i] - 48);
        }
        System.out.println(idPrefix);
        return result;
    }


    public String binaryToDecimal(Long inputNum){
        StringBuilder stb = new StringBuilder();
        for(int i=63,j=0;i>=0;j++,i--){
            stb.append (inputNum >>> i & 1);
        }
        return stb.toString();
    }



    public static void main(String[] args) {
       String sql = "CREATE TABLE `t_user_talk_mapping_%s` (`id` bigint(20) NOT NULL,`user_id` bigint(20) DEFAULT NULL,`talk_id` bigint(20) DEFAULT NULL,`create_time` datetime DEFAULT NULL,`update_time` datetime DEFAULT NULL,PRIMARY KEY (`id`),KEY `idx_user_id` (`user_id`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin COMMENT='用户说说，评论关联表';";
       for(int i=1;i<=1000;i++){
           String index = ""+i;
           if(i<10){
               index = "000"+i;
           } else if (i<100){
               index = "00"+i;
           } else if (i<1000){
               index = "0"+i;
           }
           System.out.println(String.format(sql,index));
       }
    }
}
